SWAGOLX.EXE (c) 1993 GDSOFT ALL RIGHTS RESERVED 00008 1 08-24-9413:25ALL SETH ANDERSON Binary to Integer SWAG9408 ╜╜ 26 ₧8 {πHey, recently, I've developed a binary to integer, and integer to binary,πconversion operations. This is the fastest way that I know how to write this,πshort of assembly (which I do not know at current). The original code wasπmuch longer, and much slower, yet it worked too, just slower. If you have anyπsuggestions, please let me know, I'm curious to see the results. (And, pleaseπlet me know if you find a use for this source. Right now, I only use it inπone of several units I've written, to view binary files.)ππMy programming style is very organized, so it shouldn't be too hard to follow.ππ------------------------------ CUT HERE --------------------------------------}πππTYPEπ String8 = String[8]; { For Use With The Binary Conversion }π String16 = String[16]; { For Use With The Binary Conversion }ππ Conversions = Objectπ Function Bin8ToInt ( X : String ) : Integer;π Procedure IntToBin8 ( X : Integer; VAR Binary8 : String8 );π End; { OBJECT Conversions }ππ{ I only use OOP because it sits in a unit. For a normal program, or an }π{ easy to use unit, you don't even need these three lines. I have more }π{ conversion subprograms added to this object, which is why I have an }π{ individual object for the conversion subprograms. }ππCONSTπ Bits8 : Array [1..8] of Integer = (128, 64, 32, 16, 8, 4, 2, 1);ππ{ This defines a normal 8 bits. I have a Bin16toInt and IntToBin16 }π{ procedure and function, retrorespectively, but I think that they do not }π{ have any use to them. }ππ{────────────────────────────────────────────────────────────────────────────}ππFunction Conversions.Bin8ToInt ( X : String ) : Integer;ππ{ Purpose : Converts an 8-bit Binary "Number" to an Integer. }π{ The 8-bit "Number" is really an 8-character string, or at least }π{ it should be. }ππVARπ G, Total : Integer;ππBeginπ Total := 0;π For G := 1 to 8 Doπ If ( X[G] = '1' ) thenπ Total := Total + Bits8[G];π Bin8ToInt := Total;ππEnd; { FUNCTION Conversions.Bin8ToInt }π{────────────────────────────────────────────────────────────────────────────}ππProcedure Conversions.IntToBin8 ( X : Integer;π VAR Binary8 : String8 );ππ{ Purpose : Converts an integer (from 1 to 256) to an 8-bit Binary "integer."}π{ The 8-bit "integer" is actually a string, easily convertable to }π{ an integer. }ππVARπ G : Integer;ππBeginπ Binary8 := '00000000';π For G := 1 to 8 Doπ If ( X >= Bits8[G] ) Thenπ Beginπ X := X - Bits8[G];π Binary8[G] := '1';π End;π If ( X > 0 ) Thenπ Binary8 := 'ERROR';ππEnd; { PROCEDURE Conversions.IntToBin8 }π{────────────────────────────────────────────────────────────────────────────}π 2 08-24-9413:53ALL ERIC LOWE Ramdon Integer SWAG9408 │.ß· 3 ₧8 πFunction RandomInteger: Integer; Assembler;πasmπ mov ah,2chπ int 21h { Get a random seed from DOS's clock }π imul 9821π inc axπ ror al,1π rol ah,1 { Randomize the seed }πend;π 3 08-24-9413:54ALL BRIAN RICHARDSON Random Numbers SWAG9408 ┐ù╘? 11 ₧8 {π HG> Did any one have an algorithm to generate random numbers?π HG> I know Borland Pascal have de function RANDOM but what I realyπ HG> want is the code to do that. Any Language is ok, but I preferπ HG> Pascal.ππ Here's a small random number unit that is quite good..π}ππunit Random;ππinterfaceππprocedure SeedRandomNum(ASeed : word);πprocedure InitRandom;πfunction RandomNum : word;πfunction RandomRange(ARange : word): word;ππimplementationππvarπ Fib : array[1..17] of word;π i, j : word;ππprocedure SeedRandomNum(ASeed : word);πvar x : word;πbeginπ Fib[1] := ASeed;π Fib[2] := ASeed;π for x := 3 to 17 doπ Fib[x] := Fib[x-2] + Fib[x-1];π i := 17;π j := ASeed mod 17;πend;ππprocedure InitRandom;πbeginπ SeedRandomNum(MemW[$40:$6C]);πend;ππprocedure SeedRandom(ASeed : word);πvar x : word;πbeginπ Fib[1] := ASeed;π Fib[2] := ASeed;π for x := 3 to 17 doπ Fib[x] := Fib[x-2] + Fib[x-1];π i := 17;π j := ASeed mod 17;πend;ππfunction RandomNum : word;πvar k : word;πbeginπ k := Fib[i] + Fib[j];π Fib[i] := k;π dec(i);π dec(j);π if i = 0 then i := 17;π if j = 0 then j := 17;π RandomNum := k;πend;ππfunction RandomRange(ARange : word): word;πbeginπ RandomRange := RandomNum mod ARange;πend;ππend.π 4 08-25-9409:06ALL JOSE CAMPIONE Hex encode binary files SWAG9408 1i 32 ₧8 (*************************************************************************ππ ===============================================π Hex-encode binary files in debug-script batchesπ ===============================================π Copyright (c) 1993,1994 by José Campioneπ Ottawa-Orleans Personal Systems Groupπ Fidonet: 1:163/513.3ππ This program reads a binary file and creates a hex-encoded π text file. This text file is also a batch file and a debug π script which, when run, will use debug.exe or debug.com to π reconstruct the binary file. ππ**************************************************************************)π{$M 2048,0,0}πprogram debugbat;ππuses crt,dos;ππconstπ maxsize = $FFEF;ππtypeπ string2 = string[2];ππvarπ ifile : file of byte;π ofile : text;π n : word;π s : word;π b : byte;π fsize : word;π dir : dirstr;π nam : namestr;π ext : extstr;π filename : string[12];π i : integer;ππfunction b2x(b: byte): string2;πconst hexdigit: array[0..15] of char = '0123456789ABCDEF';πbeginπ b2x:= hexdigit[b shr 4] + hexdigit[b and $0F];πend;ππprocedure myhalt(e: byte);πbeginπ gotoxy(1,wherey);π case e ofπ 0 : writeln('done.');π 1 : writeln('error in command line.');π 2 : writeln('file exceeds the 65K limit.');π else beginπ e:= 255;π writeln('Unknown error.');π end;π end;π halt(e);πend;ππbeginπ writeln;π writeln('DEBUGBAT v.1.0. Copyright (c) Feb/93 by J. Campione.');π write('Wait... ');π n := 0;π s := $F0;π {$I-}π assign(ifile,paramstr(1));π reset(ifile);π {$I+}π if (paramcount <> 1) or (ioresult <> 0) or (paramstr(1) = '') then myhalt(1);π fsplit(paramstr(1),dir,nam,ext);π for i:= 1 to length(ext) do ext[i]:= upcase(ext[i]);π for i:= 1 to length(nam) do nam[i]:= upcase(nam[i]);π if ext = '.EXE' then filename:= nam + '.EXX'π else filename:= nam + ext;π fsize:= filesize(ifile);π if fsize > maxsize then myhalt(2);π assign(ofile, nam + '.BAT');π rewrite(ofile);π writeln(ofile,'@echo off');π writeln(ofile,'rem');π writeln(ofile,'rem *************************************************************************');π writeln(ofile,'rem File ',nam + '.BAT',' was created by program DEBUGBAT.EXE v.1.0');π writeln(ofile,'rem Copyright (c) Feb. 1993 by J. Campione (1:163/513.3)');π writeln(ofile,'rem Running this file uses DEBUG to reconstruct file ',nam + ext);π writeln(ofile,'rem *************************************************************************');π writeln(ofile,'rem');π writeln(ofile,'echo DEBUGBAT v.1.0. Copyright (c) Feb/93 by J. Campione.');π writeln(ofile,'if not exist %1debug.exe goto error1');π writeln(ofile,'goto decode');π writeln(ofile,':error1');π writeln(ofile,'if not exist %1debug.com goto error2');π writeln(ofile,':decode');π writeln(ofile,'echo Wait...');π writeln(ofile,'debug < %0.BAT > nul');π writeln(ofile,'goto name');π writeln(ofile,':error2');π writeln(ofile,'echo Run %0.BAT with DEBUG''s path in the command line');π writeln(ofile,'echo example: %0 c:\dos\ ... notice the trailing slash!');π write(ofile,'goto end');π while not eof(ifile) do beginπ n:= n + 1;π read(ifile,b);π if n mod 16 = 1 then beginπ s := s + 16;π writeln(ofile);π write(ofile,'E ',b2x(hi(s)),b2x(lo(s)));π end;π write(ofile,' ',b2x(b));π end;π writeln(ofile);π writeln(ofile,'RCX');π writeln(ofile,b2x(hi(n)),b2x(lo(n)));π if ext = '.EXE' then beginπ filename:= nam + '.EXX';π end;π writeln(ofile,'N ',filename);π writeln(ofile,'W');π writeln(ofile,'Q');π writeln(ofile,':name');π if ext = '.EXE' then beginπ writeln(ofile,'if exist ',nam + ext,' del ',nam + ext);π writeln(ofile,'rename ',nam + '.EXX ',nam + ext);π end;π writeln(ofile,':end');π close(ifile);π close(ofile);π myhalt(0);πend.ππ 5 08-25-9409:10ALL CLIVE MOSES Permutinf Words SWAG9408 ÑΓ⌡O 18 ₧8 {πHere is another attempt. It will also work with any length stringπand generates all permutations without running out of memory, byπsearching in a depth-first fashion.π}ππ{$M 64000,0,655360}ππprogram perms2;ππuses Crt;ππtype str52 = string[52];ππconst objects : str52 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';ππvar m, n,π fw, level,π nperline : integer;π p1, p2 : str52;π nperms : longint;ππprocedure p (var p1, p2 : str52; var level : integer);πvar p1n, p2n : str52;π i, nlevel : integer;πbeginπ if level < mπ thenπ beginπ nlevel := level + 1;π for i := 1 to length(p2) doπ beginπ p1n := p1 + p2[i];π p2n := p2;π delete (p2n, i, 1);π p (p1n, p2n, nlevel);π end;π endπ elseπ beginπ write (p1:fw);π inc (nperms);π end;πend;ππbeginπ repeatπ clrscr;π repeatπ write ('How many objects altogether? ');π readln (n);π until (n>=0) and (n<53);π if n>0π thenπ beginπ repeatπ write ('How many in each permutation? ');π readln (m);π until (m>0) and (m<=n);π writeln;π case m ofπ 1 : fw := 2; { 40 per line }π 2..3 : fw := 4; { 20 per line }π 4 : fw := 5; { 16 per line }π 5..7 : fw := 8; { 10 per line }π 8..9 : fw := 10; { 8 per line }π 10..15 : fw := 16; { 5 per line }π 16..19 : fw := 20; { 4 per line }π 20..39 : fw := 40; { 2 per line }π 40..52 : fw := 80; { 1 per line }π end;π nperline := 80 div fw;π level := 0;π p1 := '';π p2 := copy (objects, 1, n);π nperms := 0;π p (p1, p2, level);π if (nperms mod nperline) <> 0 then writeln;π writeln;π writeln (nperms,' Permutations generated.');π readln;π end;π until n=0;πend.π{πThis one is a little more elegant, and should also be a littleπeasier to decipher than the last one! Hope this will be of someπuse to you!π}π 6 08-25-9409:10ALL CLIVE MOSES Word Permutes 2! SWAG9408 àå2ƒ 30 ₧8 {πDC>I have a little major problem... And offcourse I want YOU to help me!πDC>I want to write something that gives of a 8-letter word all the possibleπDC>combinations. So that 'RDEPTRAO' gives 'PREDATOR'. I think it must be aboutπDC>256 combinations. I don't need a program that gives 'PREDATOR' directly, butπDC>just something that gives me all those possibilities.ππHere is something that may help you a little. It works fine on myπPC with one small proviso. If you specify permutations of 8πobjects taken 8 at a time (what you want ...) then the programπruns out of heap space. Try it will smaller numbers first - likeπpermutations of 5 objects taken 3 at a time. This will show youπhow it works. You can then try to modify it so that it will notπrun out of memory generating the 40320 permutations that you areπlooking for.ππ Program perms, written by Clive Moses. This program willπ generate all permutations of n objects, taken r at a time,π memory allowing.ππ Challenge: try to modify the program so that it will notπ guzzle massive amounts of memory generating its output.π}ππprogram perms;ππ{ Program to generate permutations of n objects, taken m at a time.π For test purposes: m <= n <= 8. The program, as implemented here,π effectively uses a 'breadth-first' algorithm. If it could be changedπ to run in a 'depth-first' fashion, it would not be necessary toπ store all of the intermediate information used to create theπ permutations. A 'depth-first' algorithm might have to be recursiveπ however.π}ππuses crt;ππtype str8 = string[8];ππ torec = ^rec;ππ rec = recordπ perm,π left : str8;π next : torec;π end;ππconst objects : str8 = 'abcdefgh';ππvar m, n : integer;π first : torec;ππprocedure NewRec (var p : torec);πbeginπ NEW (p);π with p^ doπ beginπ perm := '';π left := '';π next := NIL;π end;πend;ππprocedure PrintPerms (var first : torec);πvar p : torec;π count : integer;πbeginπ p := first;π count := 0;π while p<>NIL doπ beginπ if p^.perm <> ''π thenπ beginπ write (p^.perm:8);π inc (count);π end;π p := p^.next;π end;π writeln;π writeln;π writeln (count,' records printed.');πend;ππprocedure MakePerms (m, n : integer; var first : torec);πvar i,π level : integer;π p,π p2,π temp : torec;πbeginπ writeln ('Permutations of ',n,' objects taken ',m,' at a time ...');π writeln;π if m <= nπ thenπ beginπ level := 0;π NewRec (first);π first^.left := copy (objects, 1, n);π while level < m doπ beginπ p2 := NIL;π temp := NIL;π p := first;π NewRec (p2);π while p <> NIL doπ beginπ for i := 1 to length(p^.left) doπ beginπ if temp=NIL then temp := p2;π p2^.perm := p^.perm + p^.left[i];π p2^.left := p^.left;π delete (p2^.left, i, 1);π NewRec (p2^.next);π p2 := p2^.next;π end;π p := p^.next;π end;π inc (level);π p := first;π while p<>NIL doπ beginπ p2 := p^.next;π dispose (p);π p := p2;π end;π first := temp;π endπ end;πend;ππbegin { Main Program }π clrscr;π first := NIL;π writeln ('Memory available = ',memavail);π writeln;π repeatπ write ('Total number of objects: ');π readln (n);π until n in [1..8];π repeatπ write ('Size of permutation: ');π readln (m);π until m in [1..n];π MakePerms (m, n, first);π PrintPerms (first);π writeln;π writeln ('Memory available = ',memavail);πend.π 7 08-26-9407:26ALL COLIN NICHOLSON NUM2WORD.PAS SWAG9408 ╫Xôú 40 ₧8 Unit Num2Word;π{* Program by: Richard Weber - 08/02/94 - 4 hours work *}π{* 70614,2411 *}πInterfaceππ{* BY: Richard Weber *}π{* CrazyWare - 08/02/94 *}π{* CompuServe ID: 70614,2411 *}ππ{* This program was written in 4 hours. *}ππ{* Program is self Explainatory. There is only one available function. *}π{* Function Number2Name(L : LongInt) : String; *}ππ{* If you call Number2Name(20) it will return the word equalivent *}π{* as a string. It function will process up to 2 billion and will *}π{* not process numbers less than zero or fractions of one. *}ππ{* I hope the unit comes in handy and will prevent you from working *}π{* one out form scratch. *}ππ{* Feel free to modify and expand it as will. Please leave me a message *}π{* for any questions or comments. *}πππ Function Number2Name(L : LongInt) : String;π { Function converts Long Integer supplied to a Word String }ππImplementationππCONSTπ N_Ones : Array[0..9] of String[5] =π ('',π 'One',π 'Two' ,π 'Three',π 'Four',π 'Five',π 'Six',π 'Seven',π 'Eight',π 'Nine');π N_OnesX : Array[0..9] of String[9] =π ('Ten',π 'Eleven',π 'Twelve',π 'Thirteen',π 'Fourteen',π 'Fifteen',π 'Sixteen',π 'Seventeen',π 'Eightteen',π 'Nineteen');π N_Tens : Array[2..10] of String[7] =π ('Twenty',π 'Thirty',π 'Forty',π 'Fifty',π 'Sixty',π 'Seventy',π 'Eighty',π 'Ninety',π 'Hundred');π N_Extra : Array[1..3] of String[8] =π ('Thousand',π 'Million',π 'Billion');ππ Hundred = 10; {* N_Tens[10] *}ππ Function LongVal(S : String) : LongInt;π Varπ TmpVal : LongInt;π Count : Integer;π Beginπ Val(S, TmpVal, Count);π LongVal := TmpVal;π End;ππ Function Long2Str(L : LongInt) : String;π Varπ S : String;π Beginπ Str(L,S);π Long2Str := S;π End;ππ Function Number2Name(L : LongInt) : String;π Varπ NameString : String;π NumberString : String;π Finished : Boolean;π Place : Integer;π StopPlace : Integer;π BeginPlace : Integer;π CountPlace : Integer;ππ Function Denom(I : Integer) : String;π Varπ TestPlace : Integer;ππ Beginπ TestPlace := I Div 3;π If I Mod 3 <> 0 then Inc(TestPlace);ππ If TestPlace > 1 thenπ Denom := N_Extra[TestPlace-1]π Elseπ Denom := '';π End;ππ Function TensConvert(S : String) : String;π Var TmpStr : String;π Beginπ If Length(S) > 2 then S := Copy(S,2,2);π TensConvert := '';ππ If LongVal(S) <= 19 thenπ Beginπ If LongVal(S) >=10 thenπ TensConvert := N_OnesX[LongVal(S)-10]π Elseπ TensConvert := N_Ones[LongVal(S)];π Endπ Elseπ Beginπ TmpStr := N_Tens[LongVal(S) Div 10];π If LongVal(S) Mod 10 <> 0 thenπ TmpStr := TmpStr + '-' + N_Ones[LongVal(S) Mod 10];π TensConvert := TmpStr;π End;π End;ππ Function HundredConvert(S : String; Place : BYTE) : String;π Varπ TmpString : String;ππ Beginπ TmpString := '';π If LongVal(S) > 0 thenπ Beginππ If (Length(S) = 3) and (LongVal(S[1]) > 0) thenπ TmpString := TmpString + ' ' + N_Ones[LongVal(S[1])]+π ' ' + N_Tens[Hundred];ππ TmpString := TmpString + ' ' + TensConvert(S);ππ TmpString := TmpString + ' ' + Denom(Place);ππ End;π HundredConvert := TmpString;π End;ππ Beginπ If L > 0 then π Beginπ StopPlace := 0;π Place := 3;π NameString := '';π NumberString := Long2Str(L);ππ Finished := False;π Repeatπ If Place > Length(NumberString) thenπ Beginπ Place := Length(NumberString);π Finished := True;π End;ππ IF Place <> StopPlace thenπ Beginπ BeginPlace := Length(NumberString)-Place+1;π CountPlace := Place-StopPlace;π NameString := HundredConvert(Copy(NumberString,BeginPlace,CountPlace),Place ) + NameString;π End;ππ StopPlace := Place;π Inc(Place,3);π Until Finished;ππ Number2Name := NameString;π Endπ Elseπ Number2Name := ' Zero';π End;ππBeginπEnd.ππ{ --------------- demo ------------------------- }ππProgram TestNum;πUses Num2Word;ππVarπ Lop : Integer;π Tmp : LongInt;ππBeginπ Writeln;π Randomize;π For Lop := 1 to 10 doπ Beginπ Tmp := Random(65534);π Writeln(Tmp, Number2Name(Tmp));π End;ππ Readln;πππ For Lop := 0 to 20 doπ Beginπ Writeln(Lop, Number2Name(Lop));π End;ππ Readln;πππ For Lop := 10 to 100 doπ Beginπ Writeln(Lop*10, Number2Name(Lop*10));π End;ππEnd. 8 08-26-9408:32ALL NEIL J. RUBENKING BCD Reals SWAG9408 t▀ƒ 9 ₧8 { The below is a function to convert BCD real numbers into "normal"π Turbo Reals. It runs under "normal" Turbo or Turbo-87. Very likelyπ the only use for it is to read BCD reals from a FILE and convert them.π -- Neil J. Rubenking}ππ TYPEπ RealBCD = array[0..9] of byte;ππ FUNCTION BCDtoNorm(R : realBCD) : real;π Varπ I, IntExponent : Integer;π N, Tens, Exponent : Real;π sign : integer;π BEGINπ IF R[0] = 0 THEN BCDtoNORM := 0π ELSEπ BEGINπ IntExponent := (R[0] AND $7F) - $3F;π IF R[0] AND $80 = $80 THEN Sign := -1 ELSE Sign := 1;π N := 0; Tens := 0.1;π FOR I := 9 downto 1 DOπ BEGINπ N := N + Tens*(R[I] SHR 4);π Tens := Tens * 0.1;π N := N + Tens*(R[I] AND $F);π Tens := Tens * 0.1;π END;π Exponent := 1.0;π FOR I := 1 to IntExponent DO Exponent := Exponent * 10.0;π BCDtoNORM := Exponent * N * Sign;π END;π END;π